package com.HMI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Clock extends JFrame implements ActionListener {

	private SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
	private Timer timer;
	private JLabel timeLabel;
	private MainMenu parent;
	private String date;
	
	public Clock(JLabel timeLabel, String date, MainMenu parent) {

		super();
		this.parent = parent;
		this.timeLabel = timeLabel;
		this.date = date;
		timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));

		// Set the timer to update the clock every second
		timer = new Timer(1000, this);
		timer.setRepeats(true);
		timer.start();
	}

	public void actionPerformed(ActionEvent e) {
		// If the timer caused this event.
		if (e.getSource().equals(timer)) {
			// Then set a new time.
			timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
			String today = new SimpleDateFormat("EEE, MMM d").format(Calendar.getInstance().getTime());
			if(!today.equals(date)) {
				parent.updateDate(today);
			}
			parent.refreshPopop();
			parent.updateGraphAxes();
		}
	}

}
